#include "RenzoTextureManagerGL.h"
#include "RenzoImage.h"
#include "RenzoTexture.h"

namespace Renzo
{
TextureManagerGL::TextureManagerGL(void)
{
}

TextureManagerGL::~TextureManagerGL(void)
{
}

Texture* TextureManagerGL::createTexture(Image* i) {
	Texture* t = TextureManager::createTexture(i);
	
	// create OpenGL texture object
	// TODO: how to specify texture filtering (mipmap, nearest, linear, clamp?)
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	Size size = i->getSize();
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, size.width, size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, i->getInternalData());

	// keep track of loaded textures
	texturesGL[t] = tex;

	return t;
}

void TextureManagerGL::bindTexture(Texture* t) {
	// TODO: how to keep track of texture states (glEnable(GL_TEXTURE_2D)
	glBindTexture(GL_TEXTURE_2D, texturesGL[t]);
}
}